import json
import time
import requests
from .hx_gateway_encry import hx_encry, hx_aes

Login_Server = 'http://192.168.15.143:9100/tpass_new'

datas = {'cookies': '',
         'proxies': '',
         'get_jump_url': True,
         'jk_name': 'goto_etax',
         'nsrsbh': '91320707MA1X8GCD9C',
         "login_code": "EEE9A6D340BD4A8087869AD95A067A4F",
         'dqbm': "3100",
         'STE': False,
         }


def get_requests(json_post):
    encryer = hx_encry()
    AESer = hx_aes()

    current_time = str(int(time.time() * 1000))
    encrypt_response_data = encryer.encode(json.dumps(json_post), current_time)

    current_time_eny = AESer.encrypt_by_aes(current_time, AESer.AES_key)

    # url = 'http://127.0.0.1:5000/etax_login'
    # url = 'http://192.168.15.143:9100/tpass_new'
    url = 'http://172.31.115.4:34417/tpass_new'
    req = requests.post(
        url, json={"cts": current_time_eny, "data": encrypt_response_data}, verify=False)

    res = req.json()
    current_time_jiemi = AESer.decrypt_by_aes(res['cts'], AESer.AES_key)
    json_data = encryer.decode(res['data'], current_time_jiemi)
    return json.loads(json_data)


def area_code(code_dict: dict):
    area = code_dict.get('area')
    area_dict = {'1100': 'beijing', '1200': 'tianjing', '1300': 'hebei', '1400': 'shanxi', '1500': 'neimenggu',
                 '2100': 'liaoning', '2102': 'dalian',
                 '2200': 'jilin', '2300': 'heilongjiang', '3100': 'shanghai', '3200': 'jiangsu', '3300': 'zhejiang',
                 '3302': 'ningbo', '3400': 'anhui',
                 '3500': 'fujian', '3502': 'xiamen', '3600': 'jiangxi', '3700': 'shandon', '3702': 'qindao',
                 '4100': 'henan', '4200': 'hubei',
                 '4300': 'hunan', '4400': 'guangdon', '4403': 'shenzhen', '4500': 'guangxi', '4600': 'hainan',
                 '5000': 'chongqin', '5100': 'sichuan', '5200': 'guizhou', '5300': 'yunnan', '5400': 'xizang',
                 '6100': 'shannxi', '6200': 'gansu', '6300': 'qinhai',
                 '6400': 'ningxia', '6500': 'xinjiang'}
    for k, v in area_dict.items():
        if v == area:
            code_dict.update({"dqbm": k})
    return code_dict


def get_loginCode(item: dict):
    datas.update(item)
    area_code(datas)
    return get_requests(datas)
